<?php
/*
 * msg: 
 * 1.please choose a file
 * 2.bad file type
 * 3.fail to upload to web server
 * 4.fail to upload to cdn
 * 5.large than max_file_size
 * */
require_once(dirname(__FILE__)."/../../../config/Config.inc");

class UploadHandler{	
	
	public $max_file_size = null;
	
	public function __construct(){
		$this->max_file_size = UPLOAD_MAXSIZE;
	}
	
	public function upload($filetype)
	{

		// Para init
		$cdn_url       = UPLOAD_URL;
		$lp_upload_key = UPLOAD_KEY;
		$f_path        = IMG_DIR;
		

		if(empty($_FILES[$lp_upload_key]['name'])){
			$this->output_js('1');
			return;
		}
		
		$fname = $_FILES[$lp_upload_key]['name'];
		
		//file size
		$filesize = $_FILES[$lp_upload_key]['size'];
		if(!$this->check_file_size($filesize)){
			$this->output_js('5');
			return;
		}
		// check upload file type
		$accept = $this->check_file_type($_FILES[$lp_upload_key],$filetype);
		if($accept == false){
			$this->output_js('2');
			return;
		}
		
		// get (and make dir)
		$uploads_dir = $this->get_upload_dir($f_path);	
		//兼容上传excel，上传excel验证返回临时文件目录
		if($filetype == 'excel'){
			$saved_file  = $this->save_upload_file($uploads_dir,$_FILES[$lp_upload_key]);
			if($saved_file == false){
				return false;
			}else{
				return $uploads_dir.$saved_file;
			}
			

		}else{
			// save upload file
			$saved_file  = $this->save_upload_file($uploads_dir,$_FILES[$lp_upload_key]);
			if($saved_file == false){
				$this->output_js('3');
				return;
			}
			$this->output_js('',$saved_file, $filesize,$fname);
			
		}
		
	}
	
	private function output_js($msg,$file_addr = '',$file_size=0,$fname=''){	
		if($_GET['filenum']){
			//新增加的最后一个参数为编号,文件上传后返回对应编号
			echo "<script type='text/javascript'>parent._upload_callback('$msg','$file_addr',$file_size,'$fname',{$_GET[filenum]});</script>";
		}else{
			echo "<script type='text/javascript'>parent._upload_callback('$msg','$file_addr',$file_size,'$fname');</script>";
		}
	}
	
	private function get_upload_dir($f_path){
		
		return $f_path;
	}
	
	private function save_upload_file($uploads_dir, $file_obj){
	
		$uploadfile = basename($file_obj['name']);
		
		$names = explode('.',$uploadfile);
		if(count($names) < 2){
			return false;
		}
		
		$newfile = time().'.'. $names[count($names)-1];
		$uploadfile = $uploads_dir.$newfile;

		if (move_uploaded_file($file_obj['tmp_name'], $uploadfile)) {	    
		    return $newfile;
		} else {
		    return false;
		}
	}
	
	private function check_file_type($file_obj,$filetype){
		$s_type = array(
			'img' =>array('jpg','gif','png','swf','flv','ico','jpeg','txt','doc','docx','ppt','pptx','pps','ppsx','zip','rar','mp4','rm','rmvb','mov'),
			'txt' =>array('txt','log'),
			'excel'=> array('xlsx','xls')
		);		
		$names = explode('.',basename($file_obj['name']));
		if(count($names) >= 2){
			$f_ex = strtolower($names[count($names)-1]);
			if(in_array($f_ex,$s_type[$filetype])){
				return true;	
			}
		}		
		return false;
	}
	
	private function check_file_size($size){
		if(is_numeric($this->max_file_size) && $this->max_file_size > 0){
			if($size > $this->max_file_size){
				return false;
			}
		}
		return true;
	}
}

?>